part of '../repository.dart';

const _kCustomLexicalItemDataIdKey = 'id';
const _kCustomLexicalItemDataTitleKey = 'title';
const _kCustomLexicalItemDataTranslationKey = 'translation';
const _kCustomLexicalItemDataTranscriptionKey = 'transcription';
const _kCustomLexicalItemDataUseCasesKey = 'use_cases';

class _CustomLexicalItemDataDto extends Equatable {
  const _CustomLexicalItemDataDto._internal({
    required this.id,
    required this.title,
    required this.translation,
    required this.transcription,
    required this.useCaseDtos,
  });

  final String id;
  final String title;
  final String translation;
  final String? transcription;
  final BuiltList<_CustomLexicalItemUseCaseDto> useCaseDtos;

  @override
  List<Object?> get props {
    return [
      id,
      title,
      translation,
      transcription,
      useCaseDtos,
    ];
  }

  factory _CustomLexicalItemDataDto.fromJson(Map<String, dynamic> json) {
    final useCaseMaps = List.castFrom<dynamic, Map>(json[_kCustomLexicalItemDataUseCasesKey]);

    final useCaseDtos = useCaseMaps.map((useCaseMap) {
      final useCaseJson = Map.castFrom<dynamic, dynamic, String, dynamic>(useCaseMap);
      return _CustomLexicalItemUseCaseDto.fromJson(useCaseJson);
    }).toBuiltList();

    return _CustomLexicalItemDataDto._internal(
      id: json[_kCustomLexicalItemDataIdKey],
      title: json[_kCustomLexicalItemDataTitleKey],
      translation: json[_kCustomLexicalItemDataTranslationKey],
      transcription: json[_kCustomLexicalItemDataTranscriptionKey],
      useCaseDtos: useCaseDtos,
    );
  }

  factory _CustomLexicalItemDataDto.fromEntity(BackupCustomLexicalItemData customLexicalItemData) {
    final useCaseDtos = customLexicalItemData.useCases.map((useCase) {
      return _CustomLexicalItemUseCaseDto.fromEntity(useCase);
    }).toBuiltList();

    return _CustomLexicalItemDataDto._internal(
      id: customLexicalItemData.id,
      title: customLexicalItemData.title,
      translation: customLexicalItemData.translation,
      transcription: customLexicalItemData.transcription,
      useCaseDtos: useCaseDtos,
    );
  }

  Map<String, dynamic> toJson() {
    final useCaseJsons = useCaseDtos.map((useCaseDto) {
      return useCaseDto.toJson();
    }).toList();

    final json = <String, dynamic>{
      _kCustomLexicalItemDataIdKey: id,
      _kCustomLexicalItemDataTitleKey: title,
      _kCustomLexicalItemDataTranslationKey: translation,
      _kCustomLexicalItemDataTranscriptionKey: transcription,
      _kCustomLexicalItemDataUseCasesKey: useCaseJsons,
    };

    return json;
  }

  BackupCustomLexicalItemData toEntity() {
    final useCases = useCaseDtos.map((useCaseDto) {
      return useCaseDto.toEntity();
    }).toBuiltList();

    return BackupCustomLexicalItemData(
      id: id,
      title: title,
      translation: translation,
      transcription: transcription,
      useCases: useCases,
    );
  }
}
